package com.zhiyizhen.handheld.common.sp;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.zhiyizhen.handheld.common.AppUtil;
import com.zhiyizhen.handheld.common.LogUtil;
import com.zhiyizhen.handheld.common.SerializeUtil;
import com.google.gson.Gson;

public class SpUtil {
    private SpUtil() {
    }

    private static class SpHolder {
        private SpHolder() {
        }

        private static final SharedPreferences SHARED_PREFERENCES = AppUtil.getContext().getSharedPreferences(AppUtil.getPackageName(), Context.MODE_PRIVATE);
    }

    private static SharedPreferences getSharedPref() {
        return SpHolder.SHARED_PREFERENCES;
    }

    public static void clear() {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.clear();
        editor.apply();
    }

    public static void remove(String key) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.remove(key);
        editor.apply();
    }

    public static void putString(String key, String value) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getString(String key, String defValue) {
        return getSharedPref().getString(key, defValue);
    }

    public static void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return getSharedPref().getBoolean(key, defValue);
    }

    public static void putInt(String key, int value) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static int getInt(String key, int defValue) {
        return getSharedPref().getInt(key, defValue);
    }

    public static void putFloat(String key, float value) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public static float getFloat(String key, float defValue) {
        return getSharedPref().getFloat(key, defValue);
    }

    public static void putLong(String key, long value) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static long getLong(String key, long defValue) {
        return getSharedPref().getLong(key, defValue);
    }

    public static void putIntArray(String key, int[] value) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.putString(key, new Gson().toJson(value));
        editor.apply();
    }

    public static int[] getIntArray(String key) {
        String json = getSharedPref().getString(key, null);
        if (!TextUtils.isEmpty(json)) {
            return new Gson().fromJson(json, int[].class);
        }
        return new int[]{};
    }

    /**
     * 用于保存整个对象
     */
    public static void putObject(String key, Object obj) {
        putString(key, obj == null ? null : SerializeUtil.obj2String(obj));
    }

    /**
     * 读取整个对象
     */
    public static Object getObject(String key) {
        String str = getString(key, null);
        if (!TextUtils.isEmpty(str)) {
            try {
                return SerializeUtil.string2Obj(str);
            } catch (Exception e) {
                LogUtil.e(e);
            }
        }
        return null;
    }

    public static void commit() {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.commit();
    }

}
